package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.RecordSearch;
import com.ruoyi.system.domain.vo.BarChartVo;
import com.ruoyi.system.service.IRecordSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 搜索量记录Controller
 *
 * @author admin
 * @date 2022-03-21
 */
@RestController
@RequestMapping("/system/search")
public class RecordSearchController extends BaseController {
    @Autowired
    private IRecordSearchService recordSearchService;

    /**
     * 查询搜索量记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:search:list')")
    @GetMapping("/list")
    public TableDataInfo list(RecordSearch recordSearch) {
        startPage();
        List<RecordSearch> list = recordSearchService.selectRecordSearchList(recordSearch);
        return getDataTable(list);
    }

    /**
     * 导出搜索量记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:search:export')")
    @Log(title = "搜索量记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RecordSearch recordSearch) {
        List<RecordSearch> list = recordSearchService.selectRecordSearchList(recordSearch);
        ExcelUtil<RecordSearch> util = new ExcelUtil<RecordSearch>(RecordSearch.class);
        util.exportExcel(response, list, "搜索量记录数据");
    }

    /**
     * 获取搜索量记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:search:query')")
    @GetMapping(value = "/{searchId}")
    public AjaxResult getInfo(@PathVariable("searchId") Long searchId) {
        return AjaxResult.success(recordSearchService.selectRecordSearchBySearchId(searchId));
    }

    /**
     * 新增搜索量记录
     */
    @PreAuthorize("@ss.hasPermi('system:search:add')")
    @Log(title = "搜索量记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RecordSearch recordSearch) {
        return toAjax(recordSearchService.insertRecordSearch(recordSearch));
    }

    /**
     * 修改搜索量记录
     */
    @PreAuthorize("@ss.hasPermi('system:search:edit')")
    @Log(title = "搜索量记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RecordSearch recordSearch) {
        return toAjax(recordSearchService.updateRecordSearch(recordSearch));
    }

    /**
     * 删除搜索量记录
     */
    @PreAuthorize("@ss.hasPermi('system:search:remove')")
    @Log(title = "搜索量记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{searchIds}")
    public AjaxResult remove(@PathVariable Long[] searchIds) {
        return toAjax(recordSearchService.deleteRecordSearchBySearchIds(searchIds));
    }

    // 柱状图，饼状图数据
    @GetMapping("/bar")
    public TableDataInfo bar(BarChartVo barChartVo) {
        List<BarChartVo> list = recordSearchService.selectBarChartVoList(barChartVo);
        return getDataTable(list);
    }
}
